package com.pvj.myaccount.ui.gallery;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

/**
 * 默认高仿魅族日历布局
 * Created by huanghaibin on 2017/11/15.
 */

public final class MyDefaultMonthView extends MonthView {

    private Paint mTextPaint = new Paint();
    private Paint mCurDayBgPaint = new Paint();
    private Paint mSelectedBgPaint = new Paint();


    private Paint mSchemeBasicPaint = new Paint();
    private float mRadio;
    private int mPadding;
    private float mSchemeBaseLine;

    private int mSelectColor = 0xFF3700B3;

    public MyDefaultMonthView(Context context) {
        super(context);

        mTextPaint.setTextSize(dipToPx(context, 8));
        mTextPaint.setColor(0xffFFFFFF);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setColor(0xffff0000);
        mSchemeBasicPaint.setFakeBoldText(true);
        mSchemeBasicPaint.setTextSize(dipToPx(context, 12));
        mRadio = dipToPx(getContext(), 7);
        mPadding = dipToPx(getContext(), 4);
        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
        mSchemeBaseLine = mRadio - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);


        mCurDayBgPaint.setColor(mSelectColor);
        mCurDayBgPaint.setStyle(Paint.Style.STROKE);
        mCurDayBgPaint.setStrokeWidth(dipToPx(context, 2));

        mSelectedBgPaint.setColor(mSelectColor);
        mSelectedBgPaint.setStyle(Paint.Style.FILL);
        mSelectedBgPaint.setStrokeWidth(dipToPx(context, 2));

    }

    /**
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return true 则绘制onDrawScheme，因为这里背景色不是是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        Path path = new Path();
        RectF oval = new RectF(x + mPadding, y, x + mItemWidth - mPadding, y + mItemHeight);
        path.addRoundRect(oval, 25, 25, Path.Direction.CCW);
        canvas.drawPath(path, mSelectedBgPaint);
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        int cx = x + mItemWidth / 2;
        canvas.drawText("-" + calendar.getScheme(), cx, mPadding + mTextBaseLine + y + (float) mItemHeight / 10, mSchemeBasicPaint);
    }


    /**
     * 获取字体的宽
     *
     * @param text text
     * @return return
     */
    private float getTextWidth(String text) {
        return mTextPaint.measureText(text);
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int top = y - mItemHeight / 6;
        // Log.d("MyDefaultMonthView","isSelected:"+isSelected+ " hasScheme:"+hasScheme + " calendar:"+ calendar.toString());

        if (isSelected) { // 选择
            mSelectTextPaint.setColor(0xffFFFFFF);
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, mSelectTextPaint);

            if (hasScheme) {
                mSchemeBasicPaint.setColor(0xffFFFFFF);
                canvas.drawText("-" + calendar.getScheme(), cx, mPadding + mTextBaseLine + y + (float) mItemHeight / 10, mSchemeBasicPaint);
            }
        } else {  // 未选择
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);

            if (calendar.isCurrentDay()) {
                Log.d("MyDefaultMonthView", "draw currentDay no selected bg :" + calendar.getDay());
                Path path = new Path();
                RectF oval = new RectF(x + mPadding, y, x + mItemWidth - mPadding, y + mItemHeight);
                path.addRoundRect(oval, 25, 25, Path.Direction.CCW);
                canvas.drawPath(path, mCurDayBgPaint);

                mSchemeBasicPaint.setColor(mSelectColor);
                canvas.drawText("-" + calendar.getScheme(), cx, mPadding + mTextBaseLine + y + (float) mItemHeight / 10, mSchemeBasicPaint);

                mSelectTextPaint.setColor(mSelectColor);
                canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, mSelectTextPaint);
            } else {
                if (hasScheme) {
                    mSchemeBasicPaint.setColor(0xFFFF0000);
                    canvas.drawText("-" + calendar.getScheme(), cx, mPadding + mTextBaseLine + y + (float) mItemHeight / 10, mSchemeBasicPaint);
                }
            }
        }

    }

    int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
